package com.kotlin.lib.flow

import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.cancellable
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking

fun foo(): Flow<Int> = flow {
    repeat(10) {
        kotlinx.coroutines.delay(50)
        println("send: $it")
        emit(it)
    }
}

fun main() = runBlocking {
    foo().collect {
        if (it >= 2) cancel()
        println("   collect:$it")
    }
}